<?php

namespace app\admin\model;


/**
 * 员工模型
 * Class Menu
 * @package app\admin\model
 */
class Admin extends Base
{
    protected $autoWriteTimestamp = true;//开启自动添加时间戳
    protected $insert = ['create_id','create_nick_name','create_ip','salt'];//自动添加

    //最后登录时间
    public function getLastTimeAttr($value){
        return !empty($value) ? date("Y-m-d H:i:s",$value) : "未激活";
    }

    //添加加密盐值
    protected function setSaltAttr()
    {
        return RandomString(config("setSaltLength"));
    }

    //添加ip
    protected function setCreateIpAttr()
    {
        return request()->ip();
    }

    //添加人id
    protected function setCreateIdAttr()
    {
        return $this->admin["admin_id"];
    }

    //添加昵称
    protected function setCreateNickNameAttr()
    {
        return $this->admin["nick_name"];
    }

    //获取管理员列表
    public function adminList($keyword = ""){
        return $this
            ->where(function ($q)use ($keyword){
                if(!empty($keyword)){
                    $q->whereOr("nick_name","like","%".$keyword."%")->whereOr("admin_name","like","%".$keyword."%")->whereOr("tel","like","%".$keyword."%");
                }
            })
            ->where(config("TableDeleteField"),0)//获取没有删除的
            ->where(function ($q){
                //超级管理员管理全部用户
                if($this->admin["group_id"] != config("SuperAdminGroupId")) {
                    //管理自己下级用户
                    $AdminGroup = new AdminGroup();
                    $subsetGroupIds = $AdminGroup->getSubsetGroupId($this->admin["group_id"]);//获取下级用户列表
                    if (!empty($subsetGroupIds)) {
                        $q->where("group_id", "IN", $subsetGroupIds);
                    } else {
                        $q->where("admin_id", 0);
                    }
                }
            })
            ->page($this->after_page)
            ->limit($this->after_limit)
            ->select();
    }

    //获取管理员列表总数
    public function adminListCount($keyword = ""){
        return $this
            ->where(function ($q)use ($keyword){
                if(!empty($keyword)){
                    $q->whereOr("nick_name","like","%".$keyword."%")->whereOr("admin_name","like","%".$keyword."%")->whereOr("tel","like","%".$keyword."%");
                }
            })
            ->where(config("TableDeleteField"),0)//获取没有删除的
            ->count();
    }

    //验证帐号是否存在
    public function checkName($user_name){
        return $this->whereOr("admin_name",$user_name)->whereOr("tel",$user_name)->value("is_frozen");
    }

    //验证密码是否正确
    public function checkPassword($user_name,$password){
        return $this->where(function ($q)use ($user_name){
            $q->whereOr("admin_name",$user_name)->whereOr("tel",$user_name);
        })->where("password",$password)->find();
    }

    //冻结账号
    public function loginFrozen($user_name){
        return $this->where("admin_name",$user_name)->setField("is_frozen",1);
    }

    //登录记录
    public function loginRecord($admin_id){
        return $this->mySave(["update_ip"=>request()->ip(),"last_time"=>time()],"admin_id",$admin_id);
    }

    //验证是否有权限修改
    public function checkSave($group_id,$admin_id){
        $is = false;
        //验证是否超级管理员
        if($this->admin["group_id"] != config("SuperAdminGroupId")) {
            //管理自己下级用户
            $AdminGroup = new AdminGroup();
            $subsetGroupIds = $AdminGroup->getSubsetGroupId($this->admin["group_id"]);//获取下级用户列表
            if(in_array($group_id,$subsetGroupIds)){//验证是否在该角色中
                if(!empty($admin_id)) {
                    $admin_group_id = $this->where("admin_id", $admin_id)->value("group_id");
                    if (in_array($admin_group_id, $subsetGroupIds)) {//验证是否在该角色中
                        $is = true;
                    }
                }else{
                    $is = true;
                }
            }
        }else{
            $is = true;
        }
        return $is;
    }
}